#!/bin/bash

###################################################################
# Written By:
#             Christian Kissinger
#
#   use as serversync
#
###################################################################

###################################################################
# Destination(s)
#
# If unsure of NAS IP, use the following command:
# nmap -sP 192.168.7.0/24 | awk '/^Nmap/{ip=$NF}/98:90:96/{print ip}'
#
# but replace 98:90:96 with what your NAS actually has 
# 
###################################################################
#NAS=`id -un 1000`@192.168.7.2
NAS=192.168.7.2
NFS1=/mnt/hdd1
NFS2=/mnt/hdd2

NASDIR1=$NAS:$NFS1 
NASDIR2=$NAS:$NFS2

# change depending if desktop or laptop
# This distinction mainly exists as 
# the author's desktops tend to have more
# than one screen, thus need slightly different
# configs for each.

# mostly to check if exists or not
LAPTOPDIR=$NFS1/config/laptop
DESKTOPDIR=$NFS1/config/desktop
BACKUPDIR=$NFS2/other_backups/`hostname`
LAPTOP=/config/laptop
DESKTOP=/config/desktop

# Customise if Desktop or Laptop!!!
MACHINE=$LAPTOP

STO_CONF=$NASDIR1$MACHINE
BACKUPS=$NASDIR2/other_backups/`hostname`

###################################################################
# Sources
###################################################################

# For everything!
USERHOME=/home/`id -un 1000`

# For use with primarily homeconf, and on client end
CONFDIR=$USERHOME/.config
FONTS=$USERHOME/.fonts
CONFS=$USERHOME/.*rc
OVPN=$USERHOME/.ovpn

# Programs
ARDUINO=$USERHOME/arduino-*
ECLIPSE=$USERHOME/eclipse

# Server end
SERVER_CONFDIR=$STO_CONF/.config
SERVER_FONTS=$STO_CONF/.fonts
SERVER_CONFS=$STO_CONF/.*rc
SERVER_OVPN=$STO_CONF/.ovpn

SERVER_ARDUINO=$STO_CONF/arduino-*
SERVER_ECLIPSE=$STO_CONF/eclipse

###################################################################
# shortcuts
###################################################################
USER=`id -un 1000`
DOC=Documents
PIX=Pictures
MUS=Music
VID=Videos
DWN=Downloads
ARD=Arduino

###################################################################
# rsync flags
###################################################################
RSF_TO=-chavzP
RSF_FROM=-havuzP

###################################################################
# misc
###################################################################
EDITOR=vim

###################################################################
# Functions
###################################################################
usage()
{
    echo "usage"
    echo -e "For backing up:
config files only:

    '$(basename "$0") backup config'

entire /home/user directory

    '$(basename "$0") backup home'

both:

    '$(basename "$0") backup all'

For syncing (copying files from server):
config files only:

    '$(basename "$0") sync config'

entire backed-up /home/user directory:

    '$(basename "$0") sync home'

both:

    '$(basename "$0") sync all'

Current rsync flags: 
to NAS: $RSF_TO
from NAS: $RSF_FROM

To change source and destinations:

    '$(basename "$0") edit'

For help:

    '$(basename "$0") help'

"
    exit 1
}

sync_home_to()
{
    # See if directory does not exist first
    if ssh $USER@$NAS "! [ -d  $BACKUPDIR ]"; then
       
       # Create the directory
       echo "Creating directory"
       ssh $USER@$NAS "mkdir -p $BACKUPDIR"
    
    fi

    # Sync Documents
    rsync $RSF_TO $USERHOME/$DOC $BACKUPS

    # Sync Pictures
    rsync $RSF_TO $USERHOME/$PIX $BACKUPS

    # Sync Music
    rsync $RSF_TO $USERHOME/$MUS $BACKUPS

    # Sync Videos
    rsync $RSF_TO $USERHOME/$VID $BACKUPS

    # Sync Downloads
    rsync $RSF_TO $USERHOME/$DWN $BACKUPS

    # Sync Arduino code
    rsync $RSF_TO $USERHOME/$ARD $BACKUPS
}

sync_config_to()
{
    # See if directory does not exist first
    if [[ $MACHINE == $LAPTOP ]]; then 

        if ssh $USER@$NAS "! [ -d  $LAPTOPDIR ]"; then
       
           # Create the directory
           echo "Creating directory"
           ssh $USER@$NAS "mkdir -p $LAPTOPDIR"
    
        fi

    elif [[ $MACHINE == $DESKTOP ]]; then

        if ssh $USER@$NAS "! [ -d $DESKTOPDIR ]"; then

            # Create the directory
            echo "Creating directory"
            ssh $USER@$NAS "mkdir -p $DESKTOPDIR"

        fi 

    else 
         
         echo "Don't know what to do with $MACHINE, exiting..."
         exit 1

    fi

    # Sync .config dir
    rsync $RSF_TO $CONFDIR $STO_CONF

    # Sync .fonts dir
    rsync $RSF_TO $FONTS $STO_CONF

    # Sync dotfiles
    rsync $RSF_TO $CONFS $STO_CONF

    # Sync ovpn files
    rsync $RSF_TO $OVPN $STO_CONF

    # Sync Arduino and Eclipse program directories
    rsync $RSF_TO $ARDUINO $STO_CONF
    rsync $RSF_TO $ECLIPSE $STO_CONF

    # retrieve list of installed files for eventual re-setup
    TMP=$(mktemp)
    TMP2=$(mktemp)
    apt list | grep -i 'installed' | grep -v 'code/stable' > $TMP
    cut -d/ -f1 $TMP > $TMP2
    scp $TMP2 $STO_CONF/packages
    rm $TMP $TMP2
}


sync_home_from()
{
    # If directory does not exist, just exit
    if ssh $USER@$NAS "! [ -d  $BACKUPDIR ]"; then
        echo "No such configs stored, exiting..."
        exit 1
    fi 

    # Sync Documents
    rsync $RSF_FROM $BACKUPS/$DOC $USERHOME

    # Sync Pictures
    rsync $RSF_TO $BACKUPS/$PIX $USERHOME

    # Sync Music
    rsync $RSF_TO $BACKUPS/$MUS $USERHOME

    # Sync Videos
    rsync $RSF_TO $BACKUPS/$VID $USERHOME

    # Sync Downloads
    rsync $RSF_TO $BACKUPS/$DWN $USERHOME

    # Sync Arduino code
    rsync $RSF_TO $BACKUPS/$ARD $USERHOME
}

sync_config_from()
{
    # See if directory does not exist first
    if [[ $MACHINE == $LAPTOP ]]; then 

        if ssh $USER@$NAS "! [ -d  $LAPTOPDIR ]"; then
            echo "No such configs stored, exiting..."
            exit 1
        fi

    elif [[ $MACHINE == $DESKTOP ]]; then

        if ssh $USER@$NAS "! [ -d $DESKTOPDIR ]"; then
            echo "No such configs stored, exiting..."
            exit 1
        fi 

    else 
         
         echo "Don't know what to do with $MACHINE, exiting..."
         exit 1

    fi

    # Sync .config dir
    rsync $RSF_FROM $SERVER_CONFDIR $USERHOME

    # Sync .fonts dir
    rsync $RSF_FROM $SERVER_FONTS $USERHOME

    # Sync dotfiles
    rsync $RSF_FROM $SERVER_CONFS $USERHOME

    # Sync ovpn files
    rsync $RSF_FROM $SERVER_OVPN $USERHOME
    
    # Sync Arduino and Eclipse program directories
    rsync $RSF_FROM $SERVER_ARDUINO $USERHOME
    rsync $RSF_FROM $SERVER_ECLIPSE $USERHOME

    # Install Arduino and eclipse
    # Or at least have it executable via dmenu or rofi or the likes
    sudo cp $USERHOME/$ARD/arduino /usr/bin/arduino
    sudo cp $USERHOME/$ECLIPSE/eclipse /usr/bin/eclipse

    # Install packages listed
    scp $STO_CONF/packages $USERHOME
    sudo apt install `cat $USERHOME/packages`
    rm $USERHOME/packages
}

case $1 in
    -[Hh]|--[Hh][Ee][Ll][Pp])
        echo -e "$(basename "$0") is a simple script
        which uses rsync to copy data to or from a storage
        machine, like a NAS of sorts.
        
        "

        usage 
    ;;
    
    [Bb][Aa][Cc][Kk][Uu][Pp])
        case $2 in
            [Aa][Ll][Ll])
                echo "backing up everything"
                sync_home_to
                sync_config_to
            ;;

            [Hh][Oo][Mm][Ee])
                echo "backing up home only"
                sync_home_to
            ;;

            [Cc][Oo][Nn][Ff][Ii][Gg])
                echo "backing up configs only"
                sync_config_to
            ;;

            *)
                usage
            ;;
        esac 
    ;;

    [Ss][Yy][Nn][Cc])
        case $2 in
            [Aa][Ll][Ll])
                echo "Syncing everything"
                sync_home_to
                sync_config_from
            ;;

            [Hh][Oo][Mm][Ee])
                echo "Syncing home only"
                sync_home_from
            ;;

            [Cc][Oo][Nn][Ff][Ii][Gg])
                echo "Syncing configs only"
                sync_config_from
            ;;

            *)
                usage
            ;;
        esac
    ;;

    [Ee]|[Ee][Dd][Ii][Tt]|-[Ee]|--[Ee][Dd][Ii][Tt])
        $EDITOR /usr/bin/$(basename "$0")
    ;;

    *)
        usage
    ;;
esac

exit 0
